﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Linq;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using M7;
using System.Collections;

namespace C7
{
	/// <summary>
	/// Interaction logic for MenuModule.xaml
	/// </summary>
    public partial class MenuModule : UserControl
    {
        private WindowOperations WindowOPs = new WindowOperations();
        public MenuModule()
        {
            this.InitializeComponent();
            WindowOPs.TextBoxesToHandlers(LayoutRoot);
            WindowOPs.WindowToAnimationHandlers(LayoutRoot, true);
        }

        private void MenuM_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thrStart = new Thread(delegate() { Menus.Search(); });
            thrStart.IsBackground = true;
            lock (thrStart)
            {
                thrStart.Start();
            }
            thrStart = new Thread(delegate() { MenuItem.Search(); });
            thrStart.IsBackground = true;
            thrStart.Start();
        }

        private void lbCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var query = from menu in Menus.MenusList
                        where menu.MenuName == lbCategories.SelectedItem.ToString()
                        select menu;
            List<NMenus> SelectedMenu = query.ToList();
            Menus.currentMenuID = SelectedMenu[0].MenuID;
            Thread thr = new Thread(delegate() { MenuItem.ShowMenuItems(Menus.currentMenuID); });
            thr.IsBackground = true;
            thr.Start();
        }
        bool IsOrder = false;
        private void grdPlate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Storyboard sb;
            if (IsOrder)
            {
                sb = (Storyboard)TryFindResource("sbPlateUpRev");
                lbCategories.Visibility = Visibility.Visible;
                lbMenuItems.Background = new SolidColorBrush();

                Thread thr = new Thread(delegate() { MenuItem.ShowMenuItems(Menus.currentMenuID); });
                thr.IsBackground = true;
                thr.Start();
            }
            else
            {
                sb = (Storyboard)TryFindResource("sbPlateUp");
                lbCategories.Visibility = Visibility.Collapsed;
                lbMenuItems.Background = new SolidColorBrush(Color.FromArgb(128, 255, 00, 81));

                lbMenuItems.ItemsSource = Order.lstOrders;
            }

            sb.Begin(this);
            IsOrder = !IsOrder;
        }

        ListBox dragSource = null;
        private void lbMenuItems_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            dragSource = parent;
            object data = GetObjectDataFromPoint(parent, e.GetPosition(parent));
            if (data != null)
                DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
        }

        #region Helpers
        private static object GetObjectDataFromPoint(ListBox source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement;
            if (element != null)
            {
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    data = source.ItemContainerGenerator.ItemFromContainer(element);
                    if (data == DependencyProperty.UnsetValue)
                        element = VisualTreeHelper.GetParent(element) as UIElement;
                    if (element == source)
                        return null;
                }

                if (data != DependencyProperty.UnsetValue)
                    return data;
            }

            return null;
        }
        #endregion

        private void grdPlate_Drop(object sender, DragEventArgs e)
        {
            if (IsOrder)
            {
                object data = e.Data.GetData(typeof(NMenuItem));
                Order.lstOrders.Remove((NMenuItem)data);
                lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        lbMenuItems.Items.Refresh();
                    }));
            }
            else
            {
                object data = e.Data.GetData(typeof(NMenuItem));
                Order.lstOrders.Add((NMenuItem)data);
            }
        }

        private void spOK_MouseDown(object sender, MouseButtonEventArgs e)
        {
            NOrder order = new NOrder(Properties.Settings.Default.TableID, Info.current.CustomerID);
            foreach (var menuItem in Order.lstOrders)
            {
                if (!order.lstItems.ContainsKey(menuItem.ID))
                {
                    order.lstItems.Add(menuItem.ID, new NOrderMenuItem { MenuItemID = menuItem.ID });
                }
                else
                {
                    ++order.lstItems[menuItem.ID].MenuItemQuantity;
                }
            }
            Order.Add(ref order);
        }


    }
}